# Pongathon
# Version 1.4.1
# Copyright 2014 Josef J. Polak III
# Pongathon is distributed under the terms of the GNU General Public License

# This file is part of Pongathon.

# Pongathon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pongathon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Controllers for Pongathon.
# Includes keyboard and CPU controllers

import events
import pygame

class Keyboard:
	"""Intercepts Pygame events from the keyboard."""
	def __init__(self, evManager):
		from pygame import event
		from pygame import key

		self.evManager = evManager
		self.evManager.RegisterListener(self)
		self.listenmode = 'intro'

	def Notify(self, event):
		if isinstance(event, events.Tick):
			if self.listenmode == 'intro':
				self.ListenIntro()
			elif self.listenmode == 'playing':
				self.ListenPlaying()
			elif self.listenmode == 'paused':
				self.ListenPaused()
			elif self.listenmode == 'over':
				self.ListenOver()
		elif isinstance(event, events.SetStateRequest):
			self.listenmode = event.state
	
	def ListenIntro(self):
		for event in pygame.event.get():
			ev = None
			if event.type == pygame.QUIT:
				ev = events.Quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					ev = events.Quit()
				elif event.key == pygame.K_1:
					ev = events.GameStartRequest(1)
				elif event.key == pygame.K_2:
					ev = events.GameStartRequest(2)
				elif event.key == pygame.K_t:
					ev = events.SetThemeRequest(1)
				elif event.key == pygame.K_y:
					ev = events.SetThemeRequest(2)
				elif event.key == pygame.K_w:
					ev = events.DisplayModeRequest()
				elif event.key == pygame.K_f:
					ev = events.DisplayModeRequest((640,480)
							, True)

			if ev:
				self.evManager.Post(ev)

	def ListenPlaying(self):			
		for event in pygame.event.get():
			ev = None
			if event.type == pygame.QUIT:
			 	ev = events.Quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					ev = events.Quit()
				elif event.key == pygame.K_UP:
					ev = events.PaddleMoveRequest(1, 'up')
				elif event.key == pygame.K_DOWN:
					ev = events.PaddleMoveRequest(1, 'down')
				elif event.key == pygame.K_a:
					ev = events.PaddleMoveRequest(2, 'up')
				elif event.key == pygame.K_z:
					ev = events.PaddleMoveRequest(2, 'down')
				elif event.key == pygame.K_p:
					ev = events.SetStateRequest('paused')
					
				elif event.key == pygame.K_t:
					ev = events.SetThemeRequest(1)
				elif event.key == pygame.K_y:
					ev = events.SetThemeRequest(2)
				elif event.key == pygame.K_w:
					ev = events.DisplayModeRequest()
				elif event.key == pygame.K_f:
					ev = events.DisplayModeRequest((640,480)
							, True)
			elif event.type == pygame.KEYUP:
				if event.key == pygame.K_UP:
					ev = events.PaddleMoveRequest(1, 'stop')
				elif event.key == pygame.K_DOWN:
					ev = events.PaddleMoveRequest(1, 'stop')
				elif event.key == pygame.K_a:
					ev = events.PaddleMoveRequest(2, 'stop')
				elif event.key == pygame.K_z:
					ev = events.PaddleMoveRequest(2, 'stop')

			if ev:
				self.evManager.Post(ev)

	def ListenPaused(self):
		for event in pygame.event.get():
			ev = None
			if event.type == pygame.QUIT:
			 	ev = events.Quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					ev = events.Quit()
				elif event.key == pygame.K_RETURN:
					ev = events.SetStateRequest('playing')
				elif event.key == pygame.K_t:
					ev = events.SetThemeRequest(1)
				elif event.key == pygame.K_y:
					ev = events.SetThemeRequest(2)
				elif event.key == pygame.K_w:
					ev = events.DisplayModeRequest()
				elif event.key == pygame.K_f:
					ev = events.DisplayModeRequest((640,480)
							, True)
			if ev:
				self.evManager.Post(ev)


	def ListenOver(self):
		for event in pygame.event.get():
			ev = None
			if event.type == pygame.QUIT:
			 	ev = events.Quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					ev = events.Quit()
				elif event.key == pygame.K_RETURN:
					ev = events.SetStateRequest('intro')
				elif event.key == pygame.K_t:
					ev = events.SetThemeRequest(1)
				elif event.key == pygame.K_y:
					ev = events.SetThemeRequest(2)
				elif event.key == pygame.K_w:
					ev = events.DisplayModeRequest()
				elif event.key == pygame.K_f:
					ev = events.DisplayModeRequest((640,480)
							, True)

			if ev:
				self.evManager.Post(ev)

class CPUSpinner:
	
	def __init__(self, evManager):
		from pygame.time import Clock

		self.evManager = evManager
		self.evManager.RegisterListener(self)

		self.keepGoing = True
		self.clock = pygame.time.Clock()

		# Temporary line for testing themes
		self.evManager.Post(events.SetThemeRequest(1))
	
	def Run(self):
		while self.keepGoing:
			self.clock.tick(60) # Limit to 60 fps
			event = events.Tick()
			self.evManager.Post(event)

	def Notify(self, event):
		if isinstance(event, events.Quit):
			self.keepGoing = False
